const positionView=require('../views/position.art');
const positionListView=require('../views/positionlist.art')
const positionModel=require("../models/position.js");
const BScroll = require('better-scroll');
class position{
constructor(){
    this.render()
    this.list=[]
    this.pageNo=1
    this.totalCount=0
    this.pageSize=15
}
renderer(list){
    let htmllist=positionListView({
        list
    });
    $('main ul').html(htmllist)
  
}


async render(){
    let that=this;
    let result=await positionModel.get({
        pageNo:this.pageNo
    });
    
 let html=positionView({})
   let $main = $('main')
   $main.html(html)

 let list=this.list=result.content.data.page.result  
 that.totalCount = result.content.data.page.totalCount

 this.renderer(list)
 let $imgHead = $('.head img')
 let $imgFoot = $('.foot img')
 let bScroll = new BScroll.default($('main').get(0), {
    probeType: 2
  })
  
  bScroll.scrollBy(0, -40)
  bScroll.on("scrollEnd", async function(){
      if(this.y>=0){
          $imgHead.attr('src',"/assets/images/ajax-loader.gif")
          let result=await positionModel.get({
              pageNo:1,
              pageSize:1
          })
          let {result:list}=result.content.data.page
     that.list=[...list,...that.list]
      that.renderer(that.list)
          bScroll.scrollBy(0,-40);
          $imgHead.attr('src',"/assets/images/arrow.png")
          $imgHead.removeClass('up')
      }

   if(this.maxScrollY >= this.y && Math.ceil(that.totalCount/that.pageSize) >= that.pageNo) {
       that.pageNo++
       $imgFoot.attr('src',"/assets/images/ajax-loader.gif")
       let result=await positionModel.get({
           pageNo:that.pageNo,
           pageSize:that.pageSize
       })
       let {result:list,totalCount}=result.content.data.page
       that.totalCount=totalCount
       that.list=[...that.list,...list]
          that.renderer(that.list)

          bScroll.scrollBy(0,40)
          $imgHead.attr('src', '/assets/images/arrow.png')
          $imgHead.removeClass('down')
   } 

  })

  bScroll.on('scroll', function() {
    if (this.y > 0) {
      $imgHead.addClass('up')
    }

    if (this.maxScrollY > this.y) {
      $imgFoot.addClass('down')
    }
  })
}

}

new position()